﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace VNPT.Control
{
    public class MyButtonPath : DevExpress.XtraEditors.ButtonEdit
    {
        public string PhanMoRong { get; set; }
        public string Path
        {
            get
            {
                return Text;
            }
        }
        public ePath ChonFile { get; set; }
        protected override void OnClickButton(DevExpress.XtraEditors.Drawing.EditorButtonObjectInfoArgs buttonInfo)
        {
            if (buttonInfo.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis)
            {

                switch (ChonFile)
                {
                    case ePath.OpenFile:
                        using (var brFolder = new OpenFileDialog())
                        {
                            DialogResult result = brFolder.ShowDialog();
                            if (result == DialogResult.OK)
                            {
                                EditValue = brFolder.FileName;
                            }
                        }
                        break;
                    case ePath.SaveFile:
                        using (var brFolder = new SaveFileDialog())
                        {
                            DialogResult result = brFolder.ShowDialog();
                            if (result == DialogResult.OK)
                            {
                                EditValue = brFolder.FileName;
                            }
                        }
                        break;
                    case ePath.OpenPath:
                        using (var brFolder = new FolderBrowserDialog())
                        {
                            DialogResult result = brFolder.ShowDialog();
                            if (result == DialogResult.OK)
                            {
                                if (!string.IsNullOrWhiteSpace(PhanMoRong) && System.IO.Directory.Exists(brFolder.SelectedPath))
                                {
                                    var files = Directory.GetFiles(brFolder.SelectedPath).Where(p => p.ToUpper().EndsWith(PhanMoRong.ToUpper()));
                                    if (files.Any())
                                    {
                                        EditValue = brFolder.SelectedPath;
                                    }
                                    else
                                    {
                                        DevExpress.XtraEditors.XtraMessageBox.Show(
                                            string.Format("Thư mục chọn không phù hợp, không chứa file có đuôi {0}. Vui lòng kiểm tra lại!", PhanMoRong),
                                            "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        EditValue = null;
                                    }
                                }
                                else
                                {
                                    EditValue = brFolder.SelectedPath;
                                }
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
            base.OnClickButton(buttonInfo);
        }
    }
}
